package com.game.modular.gameCore.npc.service.impl;


import com.game.modular.gameCore.cultivate.service.impl.CoreHeroServiceImpl;
import com.game.modular.gameCore.cultivate.service.impl.CoreHeroStarServiceImpl;
import com.game.modular.gameCore.map.service.impl.CoreCityServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.game.modular.gameCore.npc.mapper.CoreNpcRanksMapper;
import com.game.modular.gameCore.npc.domain.CoreNpcRanks;
import com.game.modular.gameCore.npc.service.CoreNpcRanksService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;


/**
 * (CoreNpcRanks)表服务实现类
 *
 * @author cxy
 * @since 2024-07-31 10:40:42
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CoreNpcRanksServiceImpl extends ServiceImpl<CoreNpcRanksMapper, CoreNpcRanks> implements CoreNpcRanksService {
    Cache<String, Map<Long,CoreNpcRanks>> cache = Caffeine.newBuilder().build();

    @PostConstruct
    private void init() {
        CoreNpcRanks initSelect = CoreNpcRanks.builder().build();
        List<CoreNpcRanks> list = this.list(new LambdaQueryWrapper<>(initSelect));
        log.info("开始缓存数据,数据量为:{}",list.size());
        cache.put(
                "coreNpcRanks", 
                list.stream().collect(Collectors.toMap(CoreNpcRanks::getId, CoreNpcRanks::init))
        );
        log.info("数据缓存完成");
    }

    public List<CoreNpcRanks> listCache() {
        Map<Long,CoreNpcRanks> values = cache.getIfPresent("coreNpcRanks");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return new ArrayList<>(values.values());
    }

    public Map<Long,CoreNpcRanks> MapCache() {
        Map<Long,CoreNpcRanks> values = cache.getIfPresent("coreNpcRanks");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values;
    }

    public CoreNpcRanks getCache(Long id) {
        Map<Long,CoreNpcRanks> values = cache.getIfPresent("coreNpcRanks");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values.get(id);
    }
}
